import uuid
from datetime import datetime
from django.db import models, connection
from django.conf import settings


# table 更新数据
class TableInfo(models.Model):
    id = models.BigAutoField
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=20, unique=True)
    deleted = models.BooleanField(
        default=False,
    )
    createdAt = models.DateTimeField(
        default=datetime.now,
    )
    updatedAt = models.DateTimeField(
        default=datetime.now,
    )

    class Meta:
        db_table = "TableInfo"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["createdAt"]),
            models.Index(fields=["name"]),
        ]


def get_table_datetime(table_name: str) -> datetime:
    """
    根据表名获取更新时间
    Args:
        table_name:

    Returns: 更新时间

    """
    table_info = TableInfo.objects.exists(name=table_name)
    if table_info is None:
        table_info = TableInfo.objects.create(name=table_name)
    return table_info.updatedAt


def update_table_datetime(table_name: str) -> datetime:
    """
    更新时间并获取更新时间
    Args:
        table_name: 要更新的字段

    Returns: 更新时间

    """
    exists = TableInfo.objects.filter(name=table_name).exists()
    if not exists:
        table_info = TableInfo.objects.create(name=table_name)
        return table_info.updatedAt
    else:
        now = datetime.now()
        TableInfo.objects.filter(name=table_name).update(updatedAt=now)
        return now


class BaseModel(models.Model):
    id = models.BigAutoField
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)
    deleted = models.BooleanField(
        default=False,
    )
    createdAt = models.DateTimeField(
        default=datetime.now,
    )
    updatedAt = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class WebSettings(BaseModel):
    name = models.CharField(max_length=200, default="", null=True, blank=True)  # 后台网站名称
    goods_less_count = models.IntegerField(default=5)  # 赏品少于多少件收取运费 单位：件
    goods_recycle_less_money = models.IntegerField(
        default=20_00
    )  # 赏品回收价格少于多少元收取运费 单位：分
    goods_freight_money = models.IntegerField(default=10_00)  # 运输费用 单位：分
    recharge_page_switch = models.BooleanField(default=True)  # 余额充值页面开关
    hidden_box_consume = models.IntegerField(default=20_00)  # 隐藏盒子消费 单位：分
    statistical_exclude_user_id = models.CharField(
        max_length=500, default="", null=True, blank=True
    )  # 统计排除用户id
    freight_code = models.CharField(
        max_length=200, default="", null=True, blank=True
    )  # 物流code

    class Meta:
        db_table = "WebSettings"  # 数据库表名


def get_web_settings(locked=False):
    """获取网站配置"""
    _query_set = WebSettings.objects.filter(deleted=False)
    web_settings = _query_set.first()
    if web_settings is None:
        WebSettings().save()
        web_settings = get_web_settings(locked)
    return web_settings
